<template>
  <form autocomplete="off">
    <template v-if="registerDisplay">
      <register
        @toLogin="
          () => {
            registerDisplay = false
          }
        "
      ></register>
    </template>
    <div class="container" v-else>
      <h1>欢迎登录</h1>
      <div class="form-control">
        <input type="text" id="email" v-model="submitData.email" required autocomplete="off" />
        <label for="email">
          <span style="transition-delay: 0ms">请</span>
          <span style="transition-delay: 100ms">输</span>
          <span style="transition-delay: 200ms">入</span>
          <span style="transition-delay: 250ms">邮</span>
          <span style="transition-delay: 300ms">箱</span>
        </label>
      </div>
      <div class="form-control">
        <input
          type="password"
          id="password"
          autocomplete="new-password"
          v-model="submitData.password"
          required
        />
        <label for="password">
          <span style="transition-delay: 0ms">请</span>
          <span style="transition-delay: 100ms">输</span>
          <span style="transition-delay: 200ms">入</span>
          <span style="transition-delay: 250ms">密</span>
          <span style="transition-delay: 300ms">码</span>
        </label>
      </div>
      <button class="login-btn" @click.prevent.stop="login">登录</button>
      <div class="register-text">
        还没有账号？去
        <a href="#" @click="toRegister">注册</a>
      </div>
    </div>
  </form>
</template>

<script>
import api from "@/api"
import storage from "@/utils/storage"
import { mapActions } from "vuex"
import { AppIdEnum } from "@/constant"
import register from "../register"

export default {
  name: "login",
  components: { register },
  data() {
    return {
      submitData: {},
      dialogVisible: true,
      registerDisplay: false
    }
  },
  methods: {
    ...mapActions(["updateUserInfo", "updateToken", "closeApp", "getCollects"]),
    toRegister() {
      this.registerDisplay = true
      this.dialogVisible = true
    },
    login() {
      var emailRe = /^([A-Za-z0-9_\-.])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,4})$/
      if (emailRe.test(this.submitData.email)) {
        api.login(this.submitData.email, this.submitData.password).then((result) => {
          if (result.data.status === 200) {
            storage.setItem("token", result.data.token)
            this.updateUserInfo(result.data.user)
            this.updateToken(result.data.token)
            this.closeApp(AppIdEnum.LOGIN)
            this.getCollects()
            this.dialogVisible = false
            this.$notify.success("登录成功")
          } else {
            this.$notify.error("登录失败")
          }
        })
      } else {
        this.$notify.error("邮箱格式不正确")
      }
    }
  }
}
</script>

<style scoped>
@import url("https://fonts.googleapis.com/css?family=Muli&display=swap");
form {
  width: 100%;
  height: 100%;
}
.container {
  font-family: "Muli", sans-serif;
  height: 100%;
  padding: 20px 40px;
  background-color: rgba(0, 0, 0, 0.8);
  color: #fff;
  text-align: center;
}
.container h1 {
  font-size: 24px;
  color: lightblue;
  margin-bottom: 50px;
}
.form-control {
  width: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
.form-control label {
  font-size: 14px;
  color: rgba(202, 211, 200, 0.8);
  position: absolute;
  top: 15px;
}
.form-control input {
  width: 100%;
  background-color: transparent;
  margin-bottom: 30px;
  border: 0;
  padding: 15px 0;
  font-size: 16px;
  border-bottom: 2px solid #fff;
  color: lightblue;
}
.form-control input:focus {
  outline: none;
  caret-color: lightblue;
  border-bottom: 2px solid lightblue;
}
.login-btn {
  width: 100%;
  height: 50px;
  margin: 40px auto;
  padding: 5px;
  border-radius: 6px;
  border: none;
  background: lightblue;
  font-size: 20px;
  /* font-weight: bold; */
  font-family: "Muli", sans-serif;
}
.login-btn:hover {
  background-color: #55e6c1;
}
.register-text {
  margin: 5px;
}
.register-text a {
  text-decoration: none;
  color: lightblue;
}
/* animaition */
.form-control label span {
  display: inline-block;
  transition: 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.form-control input:focus + label span,
.form-control input:valid + label span {
  color: lightblue;
  transform: translateY(-30px);
}
</style>
